﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Se7en.Admin.Common;
using Se7en.Admin.Model;
using Se7en.Admin.Business;
using ERMSys.Framework.Utilities;

namespace Se7en.Admin.Web.Controls
{
    public partial class UserEdit : UserControlBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (!ValidateInfo())
                return;
            User user = new User();
            user.FirstName = txtFirstName.Text.Trim();
            user.LastName = txtLastName.Text.Trim();
            user.LoginName = txtLoginName.Text.Trim();
            user.Password = txtPassword.Text.Trim();
            user.Email = txtEmail.Text.Trim();
            user.Active = chkActive.Checked;
            user.IsSystem = chkIsAdmin.Checked;
            if (!ValidateUnique(user))
                return;
            DBContext.Add(user);
            DBContext.SaveAllChanges();
            this.OnSuccess(sender, e);
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            if (!ValidateInfo())
                return;
            User user = new User();
            user.UserID = btnUpdate.CommandArgument.ConvertToInt();
            user.FirstName = txtFirstName.Text.Trim();
            user.LastName = txtLastName.Text.Trim();
            user.LoginName = txtLoginName.Text.Trim();
            user.Password = txtPassword.Text.Trim();
            user.Email = txtEmail.Text.Trim();
            user.Active = chkActive.Checked;
            user.IsSystem = chkIsAdmin.Checked;
            if (!ValidateUnique(user))
                return;
            DBContext.Update(user);
            DBContext.SaveAllChanges();
            this.OnSuccess(sender, e);
        }

        #region Methods

        private bool ValidateInfo()
        {
            lblErrorMsg.Text = "";
            bool isValid = true;
            if (txtPassword.Text != txtConfirmPassword.Text)
            {
                isValid = false;
                lblErrorMsg.Text += "Mật khẩu phải giống nhau.<br/>";
            }
            return isValid;
        }

        private bool ValidateUnique(User user)
        {
            bool isValid = true;
            if (DBContext.User_IsExistedLoginName(user))
            {
                isValid = false;
                lblErrorMsg.Text += string.Format(MessageRes.Duplicated, "Tên đăng nhập");
            }

            if (DBContext.User_IsExistedEmail(user))
            {
                isValid = false;
                lblErrorMsg.Text += string.Format(MessageRes.Duplicated, "Email");
            }
            return isValid;
        }

        private void ChangeMode()
        {
            btnAdd.Visible = this.Mode == Modes.Add;
            btnUpdate.Visible = this.Mode == Modes.Edit;
        }

        public  void ClearInputData()
        {
            this.Mode = Modes.Add;
            ChangeMode();
            txtFirstName.Text = "";
            txtLastName.Text = "";
            txtLoginName.Text = "";            
            txtPassword.Attributes["value"] = "";
            txtConfirmPassword.Attributes["value"] = "";
            txtEmail.Text = "";
            chkActive.Checked = false;
            chkIsAdmin.Checked = false;
        }

        public void LoadUserInfo(int userID)
        {
            this.Mode = Modes.Edit;
            ChangeMode();
            User user = DBContext.User_GetByID(userID);
            if (user == null)
                return;
            txtFirstName.Text = user.FirstName;
            txtLastName.Text = user.LastName;
            txtLoginName.Text = user.LoginName;
            txtEmail.Text = user.Email;
            chkActive.Checked = user.Active;
            chkIsAdmin.Checked = user.IsSystem;
            btnUpdate.CommandArgument = userID.ToString();
        }

        #endregion
    }
}